package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.UserDao;
import entity.Movie;
import entity.User;

public class UserDaoImpl implements UserDao {

	public boolean findUser(String username, String password) {
		boolean isValid=false;
		Connection conn=utils.DBUtil2.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ps=conn.prepareStatement("select * from user where u_name=? and u_password=?");
			ps.setString(1, username);
			ps.setString(2, password);
			rs=ps.executeQuery();
			while(rs.next()){
				 isValid = true; 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);	
		}
		return isValid;
	}

	public int insertUser(User u) {
		Connection conn=utils.DBUtil2.getConnection();
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement("insert into user(u_name,u_password,u_sex,u_age,u_hobby,u_describe)  values(?,?,?,?,?,?)");
			ps.setString(1, u.getU_name());
			ps.setString(2, u.getU_password());
			ps.setInt(3, u.getU_sex());
			ps.setInt(4, u.getU_age());
			ps.setString(5, u.getU_hobby());
			ps.setString(6, u.getU_describe());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(null, ps, conn);
		}
		return 0;
	}

	public User selectUser(String username) {
		User u=new User();
		Connection conn=utils.DBUtil2.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ps=conn.prepareStatement("select * from user where u_name=?");
			ps.setString(1, username);
			rs=ps.executeQuery();
			while(rs.next()){
				 u.setU_name(rs.getString(2));
				 u.setU_password(rs.getString(3));
				 u.setU_sex(rs.getInt(4));
				 u.setU_age(rs.getInt(5));
				 u.setU_hobby(rs.getString(6));
				 u.setU_describe(rs.getString(7));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			utils.DBUtil2.closeConnection(rs, ps, conn);	
		}
		return u;
	}

}
